// pages/location/location.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      province:'',
      address:'',
      markers:'',
      longitude:'',
      latitude:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key:'A4ABZ-K7FRW-YFTRE-OID7S-4XEJF-EVFET'
  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let vm = this;
    vm.getUserLocation();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }, 
  getUserLocation:function()
  {
    let vm = this;
    
    vm.getLocation();

    wx.getSetting({
      success(res) {
        console.log(res.authSetting)
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation']!= true)
        {
          wx.showModal({
            title: '请求授权地理位置',
            content: '请确认授权',
            success:function(res){
              if(res.cancel)
              {
                wx.showToast({
                  title: '拒绝授权',
                  icon:'none',
                  duration:1000
                })
              }
              else if (res.confirm)
              {
                wx.openSetting({
                  succes:function(res){
                    if (res.authSetting['scope.userLocation'] == true)
                    {
                      wx.showToast({
                        title: '授权成功',
                        duration:1000
                      })
                      vm.getLocation();
                    }else{
                      wx.showToast({
                        title: '授权失败',
                        icon:'none',
                        duration:1000
                      })
                    }
                  }
                })
              }
            }
          })
        }
        else if (res.authSetting['scope.userLocation']== undefined){
          vm.getLocation();
        }else{
          vm.getLocation();
        }
      }
    })
  },
  getLocation:function()
  {
    let vm = this;

    wx.getLocation({
      type: 'gcj02',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        vm.getRealLocal(latitude, longitude);
      }
    })
    
  },
  getRealLocal:function(latitude,longitude){
    let vm = this;
    qqmapsdk.reverseGeocoder({
        location: {
          latitude: latitude,
          longitude: longitude
        },
        success:function(res){
          let prov = res.result.ad_info.province
          let addr = res.result.formatted_addresses.recommend
          vm.setData({
            province:prov,
            address:addr,
            longitude:longitude,
            latitude:latitude
          })
        },
        fail:function(res){
          console.log(res)
        }
  })

}})